<?php
namespace catchAdmin\externalGoods\model;

use catchAdmin\externalGoods\model\search\GoodsSearch;
use catcher\base\CatchModel;
use catchAdmin\externalGoods\model\GoodsRule as externalGoodsRuleModel;
use think\Exception;
use think\facade\Db;
use catchAdmin\externalGoods\model\GoodsCustoms as GoodsCustomsModel;
class Goods extends CatchModel
{
    use GoodsSearch;

    protected $name = 'db_syst_sync_goods';
    protected $field = [
        'id',
        'ext_id',
        'goods_spu',
        'goods_name',
        'goods_subtitle',
        'goods_keywords',
        'goods_img',
        'goods_slideshow_img',
        'barcode',
        'brand_id',
        'brand_name',
        'sync_brand_id',
        'sync_brand_name',
        'origin_id',
        'origin_name',
        'trade_type_id',
        'trade_type_name',
        'unit_name',
        'unit_id',
        'is_sale',
        'is_sys_api',
        'category_id',
        'sync_category_id',
        'sync_category_id_path',
        'sync_category_name',
        'create_mode',
        'supplier_id',
        'created_at',
        'updated_at',
        'deleted_at',
        'tenant_id',
        'creator_id',
        'spec',
        'price',
        'cost_price',
        'market_price',
        'weight',
        'stock',
        'description',
        'is_ths',
        'is_upload_remote'
    ];

    public function getList($tenant_id = 0)
    {
//        $where = [];
//        $fields = 'created_at,updated_at,deleted_at,tenant_id,creator_id,create_mode';
//        if($tenant_id !== 0){
//            $where[]=['tenant_id','=',$tenant_id];
//        }else{
//            $fields = 'created_at,updated_at,deleted_at,tenant_id,creator_id,create_mode,cost_price';
//        }

        $where = [];
        $fields = 'created_at,updated_at,deleted_at,tenant_id,creator_id,create_mode,cost_price';
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }else{
            $fields = 'created_at,updated_at,deleted_at,tenant_id,creator_id,create_mode';
        }

        return $this
            ->alias('sg')
            ->field([
                'sg.id',
                'sg.ext_id',
                'sg.goods_spu',
                'sg.goods_name',
                'sg.goods_subtitle',
                'sg.goods_keywords',
                'sg.goods_img',
                'sg.goods_slideshow_img',
                'sg.barcode',
                'sg.brand_id',
                'sg.brand_name',
                'sg.sync_brand_id',
                'sg.sync_brand_name',
                'sg.origin_id',
                'sg.origin_name',
                'sg.trade_type_id',
                'sg.trade_type_name',
                'sg.unit_name',
                'sg.unit_id',
                'sg.is_sale',
                'sg.is_sys_api',
                'sg.category_id',
                'sg.sync_category_id',
                'sg.sync_category_id_path',
                'sg.sync_category_name',
                'sg.create_mode',
                'sg.supplier_id',
                'sg.created_at',
                'sg.spec',
                'sg.price',
                'sg.cost_price',
                'sg.market_price',
                'sg.weight',
                'sg.stock',
                'sg.description',
                'sg.is_ths',
                'sg.is_upload_remote'
            ])
            ->catchSearch()
            ->where($where)
            ->order('sg.id', 'desc')
            ->paginate()
            ->each(function (&$item) use ($fields)  {
                $item['goods_pic'] = $item['goods_img'];//主图
                $goods_slideshow_img = array_merge([$item['goods_pic']], json_decode($item['goods_slideshow_img'],true));
                $item['goods_slideshow_img'] =$goods_slideshow_img;//轮播图包含主图

                $item['attr'] = (new GoodsAttribute)
                    ->withoutField($fields)
                    ->where('goods_id',$item['id'])->select()->toArray();
                $item['rule'] = (new externalGoodsRuleModel)
                    ->withoutField($fields)
                    ->where('goods_id',$item['id'])->select()->toArray();

                $crossBorderParams = (new GoodsCustomsModel)->field([
                    'hg_itemRecordNo',
                    'hg_gcode',
                    'hg_gname',
                    'hg_gmodel',
                    'hg_originCountry',
                    'hg_qty1',
                    'hg_unit',
                    'hg_unit1',
                    'hg_unit2',
                    'manifest_template_id'
                ])
                    ->where('goods_id',$item['id'])->find();
                if($crossBorderParams){
                    $item['crossBorderParams'] = $crossBorderParams->toArray();
                }else{
                    $item['crossBorderParams'] = [];
                }

//                $http_url = 'https://img.qekj.net/';
//
//                $item['goods_img'] = substr($item['goods_img'], 0, 4) === "http" ? $item['goods_img'] : $http_url . $item['goods_img'];
//                $item['goods_pic'] = $item['goods_img'];
//
//                $temp = [];
//                foreach ($item['goods_slideshow_img'] as $v){
//                   $temp[] =  substr($v, 0, 4) === "http" ? $v : $http_url . $v;
//                }
//                $item['goods_slideshow_img'] = $temp;

                //分类层级
                $item['cate_id_arr'] = (new Category)->where('id',$item['category_id'])->find()->cate_id_arr ?? [];
            });
    }

    public function getList2()
    {
        $where = [];
        $fields = 'created_at,updated_at,deleted_at,tenant_id,creator_id,create_mode';

        return $this
            ->alias('sg')
            ->field([
                'sg.id',
                'sg.ext_id',
                'sg.goods_spu',
                'sg.goods_name',
                'sg.goods_subtitle',
                'sg.goods_keywords',
                'sg.goods_img',
                'sg.goods_slideshow_img',
                'sg.barcode',
                'sg.brand_id',
                'sg.brand_name',
                'sg.sync_brand_id',
                'sg.sync_brand_name',
                'sg.origin_id',
                'sg.origin_name',
                'sg.trade_type_id',
                'sg.trade_type_name',
                'sg.unit_name',
                'sg.unit_id',
                'sg.is_sale',
                'sg.is_sys_api',
                'sg.category_id',
                'sg.sync_category_id',
                'sg.sync_category_id_path',
                'sg.sync_category_name',
                'sg.create_mode',
                'sg.supplier_id',
                'sg.created_at',
                'sg.spec',
                'sg.price',
                'sg.cost_price',
                'sg.market_price',
                'sg.weight',
                'sg.stock',
                'sg.description',
                'sg.is_ths',
                'sg.is_upload_remote'
            ])
            ->catchSearch()
            ->where($where)
            ->order('sg.id', 'desc')
            ->paginate()
            ->each(function (&$item) use ($fields)  {
                $item['goods_pic'] = $item['goods_img'];//主图
                $goods_slideshow_img = array_merge([$item['goods_pic']], json_decode($item['goods_slideshow_img'],true));
                $item['goods_slideshow_img'] =$goods_slideshow_img;//轮播图包含主图

                $item['attr'] = (new GoodsAttribute)
                    ->withoutField($fields)
                    ->where('goods_id',$item['id'])->select()->toArray();
                $item['rule'] = (new externalGoodsRuleModel)
                    ->withoutField($fields)
                    ->where('goods_id',$item['id'])->select()->toArray();

                //分类层级
                $item['cate_id_arr'] = (new Category)->where('id',$item['category_id'])->find()->cate_id_arr ?? [];
            });
    }

    public function checkUnique($where, $id = 0, $pk = 'id')
    {
        try {
            if (empty($id)) {
                $has = $this->field($pk)->where($where)->find();
            } else {
                $has = $this->field($pk)->where($where)->where($pk, '<>', $id)->find();
            }
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        return $has;
    }

    public static function getTradeTypeIdBySpu($goods_spu){
        return self::where(['goods_spu'=>$goods_spu])->value('trade_type_id');
    }
}
